<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 需求：http://www.baidu.com?name=zhangsan&age=18&sex=nan#， 封装一个函数，将该地址里的参数解析出来
        // http://www.baidu.com?name=zhangsan&age=18&sex=nan#
        // 解析出
        // {name: 'zhangsan',age: 18, sex: 'nan' }
        //自己写的方法
        var str = "http://www.baidu.com?name=zhangsan&age=18&sex=nan#"
        const processes = (str) => {
            const index = str.indexOf("?")
            const newString = str.slice(index + 1, -1)
            const newArr = newString.split("&")
            const newArrTwo = newArr.map((item) => {
                console.log(item.replace("=", ":"))
                return item.replace("=", ":")
            })
            let obj = {}
            const objArr = newArrTwo.map((item) => {
                let itemArr = item.split(":")
                return obj[itemArr[0]] = itemArr[1]
            })
            console.log(obj)
        }
        processes(str)
        // 第二种方法
        const process = (url) => {
            const index1 = url.indexOf('?'),
                index2 = url.indexOf('#'),
                params = url.slice(index1 + 1, index2).split('&')
            return params.reduce((pre, curr) => {
                // [name,zhangsan]
                // { name:zhangsan } 
                const [key, value] = curr.split('=')
                pre[key] = value
                return pre
            }, {})
        }
    </script>
</body>

</html>